package com.zdp.io.bio.test1;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Time;

/**
 * @Author zhengdp
 * @Description
 */
public class TimeServer {

    public static void main(String[] args) {
        int port = 8888;
        if (args != null && args.length > 0) {
            port = Integer.parseInt(args[0]);
        }
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("The time server is start in port : " + port);
            Socket accept = null;
            while (true) {
                accept = serverSocket.accept();
                new Thread(new TimeServerThreadHandler(accept)).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭服务器
            try {
                if (serverSocket != null) {
                    System.out.println("The time server close ! ");
                    serverSocket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
